// 工具类统一导出

// 日志工具
export { default as logger, Logger, LogLevel } from './logger';

// 类型定义
export * from './types';

// 验证工具
export { ValidationUtils } from './validation';

// 安全工具
export { SecurityUtils } from './security';

// 文件工具
export { FileUtils } from './fileUtils';

// 辅助工具
export {
  delay,
  retry,
  debounce,
  throttle,
  deepClone,
  deepMerge,
  uniqueArray,
  groupBy,
  chunk,
  randomChoice,
  shuffle,
  StringUtils,
  NumberUtils,
  DateUtils,
  UrlUtils,
  PerformanceUtils,
  EnvUtils
} from './helpers';

// 常用工具函数快捷导出
export const utils = {
  // 字符串工具
  string: {
    capitalize: (str: string) => str.charAt(0).toUpperCase() + str.slice(1).toLowerCase(),
    isEmpty: (str: string | null | undefined) => !str || str.trim().length === 0,
    truncate: (str: string, length: number, suffix = '...') => 
      str.length <= length ? str : str.substring(0, length - suffix.length) + suffix,
    random: (length = 8) => Math.random().toString(36).substring(2, length + 2)
  },

  // 数字工具
  number: {
    formatBytes: (bytes: number, decimals = 2) => {
      if (bytes === 0) return '0 Bytes';
      const k = 1024;
      const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
      const i = Math.floor(Math.log(bytes) / Math.log(k));
      return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + ' ' + sizes[i];
    },
    clamp: (num: number, min: number, max: number) => Math.min(Math.max(num, min), max),
    randomInt: (min: number, max: number) => Math.floor(Math.random() * (max - min + 1)) + min
  },

  // 日期工具
  date: {
    format: (date: Date, format = 'YYYY-MM-DD HH:mm:ss') => {
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      const hours = String(date.getHours()).padStart(2, '0');
      const minutes = String(date.getMinutes()).padStart(2, '0');
      const seconds = String(date.getSeconds()).padStart(2, '0');

      return format
        .replace('YYYY', String(year))
        .replace('MM', month)
        .replace('DD', day)
        .replace('HH', hours)
        .replace('mm', minutes)
        .replace('ss', seconds);
    },
    now: () => new Date(),
    timestamp: () => Date.now()
  },

  // 数组工具
  array: {
    unique: <T>(array: T[]) => [...new Set(array)],
    chunk: <T>(array: T[], size: number) => {
      const chunks: T[][] = [];
      for (let i = 0; i < array.length; i += size) {
        chunks.push(array.slice(i, i + size));
      }
      return chunks;
    },
    shuffle: <T>(array: T[]) => {
      const shuffled = [...array];
      for (let i = shuffled.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
      }
      return shuffled;
    }
  },

  // 对象工具
  object: {
    isEmpty: (obj: any) => obj && Object.keys(obj).length === 0,
    pick: <T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Pick<T, K> => {
      const result = {} as Pick<T, K>;
      keys.forEach(key => {
        if (key in obj) {
          result[key] = obj[key];
        }
      });
      return result;
    },
    omit: <T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Omit<T, K> => {
      const result = { ...obj };
      keys.forEach(key => {
        delete result[key];
      });
      return result;
    }
  },

  // 异步工具
  async: {
    delay: (ms: number) => new Promise(resolve => setTimeout(resolve, ms)),
    timeout: <T>(promise: Promise<T>, ms: number) => {
      return Promise.race([
        promise,
        new Promise<never>((_, reject) => 
          setTimeout(() => reject(new Error('Operation timed out')), ms)
        )
      ]);
    }
  }
};